package laos.src;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.View;

class AnimationView extends View {

	public final int PLAYER_RADIUS = 8;
	public final int TAIL_SIZE = 7;
	public final int AREA_SIZE = 10;
	public final int FPS_TEXT_SIZE = 14;
	public final int PLAYER_AREA_TEXT_SIZE = 18;

	private double widthRatio;
	private double heightRatio;
	public String text;
	/** How to display the text */
	private Paint textPaint;
	/** How to display the game */
	private Paint gamePaint;
	/** The Players */
	private ArrayList<Player> players;

	/**
	 * Draws to the provided Canvas.
	 */
	@Override
	protected void onDraw(Canvas canvas) {

		// Draw the background color. Operations on the Canvas accumulate
		// so this is like clearing the screen. In a real game you can
		// put in a background image of course
		canvas.drawColor(Color.TRANSPARENT);
		synchronized (players) {
			drawTails(canvas);
			drawAreas(canvas);
			drawPlayers(canvas);
			drawAreaPercentage(canvas);
		}

		// Draw fps center screen
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(FPS_TEXT_SIZE);
		canvas.drawText(canvas.getWidth() + "*" + canvas.getHeight(),
				canvas.getWidth() - (FPS_TEXT_SIZE * 4), FPS_TEXT_SIZE * 2 + 2,
				textPaint);

		canvas.drawText("Status: " + text, 0, FPS_TEXT_SIZE, textPaint);
		canvas.drawText("H:" + String.format("%.2f", heightRatio) + ",  W:"
				+ String.format("%.2f", widthRatio), 0, FPS_TEXT_SIZE * 2 + 2,
				textPaint);

		canvas.restore();
	}

	private boolean withinScreen(Canvas c, int x, int y) {
		return (x < c.getWidth() && x >= 0 && y < c.getHeight() && y >= 0);
	}

	/**
	 * Go through the players list and draw them
	 */
	private void drawPlayers(Canvas canvas) {
		gamePaint.setAntiAlias(true);
		Iterator<Player> it = players.iterator();
		while (it.hasNext()) {
			Player p = it.next();
			Point pLoc = p.getLoc();
			int x = (int) (pLoc.x * widthRatio);
			int y = (int) (pLoc.y * heightRatio);
			if (withinScreen(canvas, x, y)) {
				gamePaint.setColor(p.getPColor());
				canvas.drawCircle(x, y, PLAYER_RADIUS, gamePaint);
			}
		}
		gamePaint.setAntiAlias(false);
	}

	/**
	 * Draws the percentage of area taken by each player
	 */
	private void drawAreaPercentage(Canvas canvas) {
		Iterator<Player> it = players.iterator();
		int size = 80;
		int locJmp = canvas.getWidth() / (players.size() - 1) - size;
		int i = 0;
		textPaint.setTextSize(PLAYER_AREA_TEXT_SIZE);
		textPaint.setAntiAlias(true);
		while (it.hasNext()) {
			Player p = it.next();
			textPaint.setColor(p.getPColor());
			canvas.drawText(
					String.format(
							"%.2f",
							(((double) p.getAreaSize() / (((double) canvas
									.getWidth() / widthRatio) * ((double) canvas
									.getHeight() / heightRatio))) * 100))
							+ "%", 0 + locJmp * i, canvas.getHeight(),
					textPaint);
			++i;
		}
		textPaint.setAntiAlias(false);
	}

	/**
	 * Go through the players list and draw them
	 */
	private void drawTails(Canvas canvas) {
		gamePaint.setAntiAlias(true);
		float tmp = gamePaint.getStrokeWidth();
		gamePaint.setStrokeWidth(TAIL_SIZE);
		Iterator<Player> it = players.iterator();
		while (it.hasNext()) {
			Player p = it.next();
			gamePaint.setColor(p.getTColor());
			Iterator<Point> it2 = p.getTail().iterator();
			Point prev = null;
			if (it2.hasNext()) {
				prev = it2.next();
				if (prev.x >= canvas.getWidth())
					prev.x = canvas.getWidth() - 1;
				if (prev.x < 0)
					prev.x = 0;
				if (prev.y >= canvas.getHeight())
					prev.y = canvas.getHeight() - 1;
				if (prev.y < 0)
					prev.y = 0;
			}
			while (it2.hasNext()) {
				Point p2 = it2.next();
				if (p2.x >= canvas.getWidth())
					p2.x = canvas.getWidth() - 1;
				if (p2.x < 0)
					p2.x = 0;
				if (p2.y >= canvas.getHeight())
					p2.y = canvas.getHeight() - 1;
				if (p2.y < 0)
					p2.y = 0;
				canvas.drawLine((int) (prev.x * widthRatio),
						(int) (prev.y * heightRatio),
						(int) (p2.x * widthRatio), (int) (p2.y * heightRatio),
						gamePaint);
				prev = p2;
			}

		}
		gamePaint.setStrokeWidth(tmp);
		gamePaint.setAntiAlias(false);
	}

	/**
	 * Go through the players list and draw them
	 */
	private void drawAreas(Canvas canvas) {
		gamePaint.setAntiAlias(true);
		Iterator<Player> it = players.iterator();
		while (it.hasNext()) {
			Player p = it.next();
			gamePaint.setColor(p.getAColor());
			Iterator<Point> it2 = p.getArea().iterator();
			while (it2.hasNext()) {
				Point p2 = it2.next();
				int x = (int) (p2.x * widthRatio);
				int y = (int) (p2.y * heightRatio);
				if (withinScreen(canvas, x, y)) {
					canvas.drawPoint(x, y, gamePaint);
					canvas.drawRect((int) (p2.x * widthRatio),
							(int) (p2.y * heightRatio), (int) (p2.x
									* widthRatio + widthRatio), (int) (p2.y
									* heightRatio + heightRatio), gamePaint);
				}
			}
		}
		gamePaint.setAntiAlias(false);
	}

	public AnimationView(Context context, ArrayList<Player> playerList) {
		super(context);
		widthRatio = 0;
		heightRatio = 0;
		text = "Waiting for opponent";
		players = playerList;
		/** Initiate the text painter */
		textPaint = new Paint();
		/** Initiate the game painter */
		gamePaint = new Paint();
	}
	
	public void setRatio(int arrWidth, int arrHeight){
		widthRatio = (double) getWidth() / arrWidth;
		heightRatio = (double) getHeight() / arrHeight;
	}
	
	public void update(ArrayList<Player> playerList){
		players = playerList;
		invalidate();
	}

}
